﻿<?php 
$req_path="./";
include_once($req_path."include/ini.php");
include_once($req_path."include/db.php");

class M_Permission extends DB{
	private $dbConn;
	private $rowCount="0";
	private $sqlSelect ="select *, DATE_FORMAT( OpenDate, '%d/%m/%Y' ) OpenedDate1 from tb_branch where IsActived=1";
	private $branchID="0";
	private $totalpage="0";
	
	function M_Permission(){
		parent::__construct();
		//$this->dbConn=$this->getDB();
	}
	function getText(){
		echo "TEST";
	}
	function getListPage(){
		global $list_page;
		return $list_page;
	}
	//Check duplicate branch cdoe 
	function checkBranch($code){
		$sql="select * from tb_branch where BranchCode='".$code."'";
		$result=$this->ExecuteData($sql);
		$rows=$result->RecordCount();
		$val=$rows > 0? "0":"1";
		return $val;
	}
	
	function getBranch($code=null, $name=null,$pageStart=1){
		global $list_page;
		$sqlscript= $this->sqlSelect;
		
		if (isset($code) && $code!=""){
			$sqlscript.= " and BranchCode='". $code."'";
		}
		if (isset($name) && $name!=""){
			$sqlscript.= " and BranchName Like '%". $name."%'";
		}
		//Order 
		$sqlscript.= " order by ID asc " ;
		
		$re = $this->ExecuteData($sqlscript);
		$rows =$re->RecordCount();
		
		//Page limit
		$sqlscript.= " limit ". ($pageStart-1)*$list_page. ", " . $list_page;
		
		//paging
		$result = $this->ExecuteData($sqlscript);
		 
		$totalpage=1;
		if ($rows>0){
			$rt = $rows%$list_page;
			
			if($rt!=0) {
				$totalpage = floor($rows/$list_page)+1;
			}
			else {
				$totalpage = floor($rows/$list_page);
			}
		}
		
		$this->rowCount=$rows;
		$this->totalpage=$totalpage;
		return $result;
	}
	
	function getBranchDetail($id=0){
		$sqlscript= "SELECT ID , BranchCode, BranchName, Address,Telephone ";
		$sqlscript.=" , DATE_FORMAT( OpenDate, '%d/%m/%Y' ) OpenedDate,Type , ContactName, ContactTele,ContactEmail ";
		$sqlscript.= " from tb_branch where IsActived=1 ";
		$sqlscript.= " and ID='".$id."'";
		$result = $this->ExecuteData($sqlscript);
		return $result->FetchRow();
	}

	function getRowCount(){
		return $this->rowCount;
	}
	
	function getLastInsert(){
		return $this->branchID;
	}
	
	function InsertBranch($val){
		if (isset($val)){
			$sqlInsert=$this->InsertSqlScript($this->sqlSelect, $val);
			$this->ExecuteData($sqlInsert);
			$this->branchID = $this->getLastID();
			return $this->branchID;
		}
	}
	function updateBranch($sql, $val){
		if(isset($val)){
			$sqlUpdate=$this->UpdateSqlScript($sql,$val);
			return $this->ExecuteData($sqlUpdate);
		}
	}
	function deleBranch($id){
		if(isset($id)){
			$sqlDelete = "update tb_branch set IsActived=0 where ID='".$id."'";
			$this->ExecuteData($sqlDelete);
			return "c";
		}
	}
	function getTotalPage(){
		return $this->totalpage;
		// global $list_page;
		
		// $result = $this->ExecuteData($this->sqlSelect);
		// $NRow=$result->RecordCount();
		// $totalpage=0;
		// if($NRow>0){
			 // $i = 1;
			 // $rt = $NRow%$list_page;
			 
			// if($rt!=0) {
				// $totalpage = floor($NRow/$list_page)+1;
			// }
			// else {
				// $totalpage = floor($NRow/$list_page);
			// }
		// }
			
		// $val= array('row'=>$NRow ,'totalpage'=>$totalpage);
		// return $val;
	}
}
?>